#parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""

# make alice keys

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/alice_monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str


str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture $permutation_str \
    --make-start-point -start-point $output_dir/alice_start_point_file.xf 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -start-point $output_dir/alice_start_point_file.xf -monomial-key $output_dir/alice_monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --make-binomial-key -binomial-key $output_dir/alice_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

# make bob keys

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/bob_monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -start-point $output_dir/alice_start_point_file.xf -monomial-key $output_dir/bob_monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --make-binomial-key -binomial-key $output_dir/bob_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

# make decoys

for i in {0..4}
do

    str="
    xrnlib-cli \
        --encoding-conf -compiled-param -no-password \
        --permutation-conf -arch $architecture $permutation_str \
        --make-monomial-key -monomial-key $output_dir/decoy_monomial_key_file_$i.xf 
    "
    echo ""; echo $str; echo ""
    eval $str

    str="
        xrnlib-cli \
            --decoding-conf -compiled-param -no-password \
            --encoding-conf -compiled-param -no-password \
            --arithmetic-conf -start-point $output_dir/alice_start_point_file.xf -monomial-key $output_dir/decoy_monomial_key_file_$i.xf \
            --sequence-conf -xmatrix $matrix_decompressed_file \
            --permutation-conf -arch $architecture \
            --make-binomial-key -binomial-key $output_dir/decoy_binomial_key_file_$i.xf
    "
    echo ""; echo $str; echo ""
    eval $str

done

# make ring signature

decoy_str=""
for i in {0..4}
do
    decoy_str="$decoy_str -add-decoy-binomial-key $output_dir/decoy_binomial_key_file_$i.xf"
done

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/alice_monomial_key_file.xf -binomial-key $output_dir/alice_binomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --make-ring-signature -plain-text $lorem_ipsum_file -verifier-binomial-key $output_dir/bob_binomial_key_file.xf -ring-signature $output_dir/alice_ring_signature_file.xf $decoy_str
"
echo ""; echo $str; echo ""
eval $str

# check ring signature

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/bob_monomial_key_file.xf -ring-signature $output_dir/alice_ring_signature_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/lib_info_log_file.xf \
    --check-ring-signature -plain-text $lorem_ipsum_file -signer-binomial-key $output_dir/signer_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_log_file.xf ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/lib_info_log_file.xf\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/signer_binomial_key_file.json \
    --print-header -xrn-file $output_dir/signer_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/alice_binomial_key_file.json \
    --print-header -xrn-file $output_dir/alice_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
if cmp --silent $output_dir/alice_binomial_key_file.json $output_dir/signer_binomial_key_file.json; then echo 'PASS'; else echo 'FAIL'; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# make ring signature

decoy_str=""
for i in {0..4}
do
    decoy_str="$decoy_str -add-decoy-binomial-key $output_dir/decoy_binomial_key_file_$i.xf"
done

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/bob_monomial_key_file.xf -binomial-key $output_dir/bob_binomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --make-ring-signature -plain-text $lorem_ipsum_file -verifier-binomial-key $output_dir/alice_binomial_key_file.xf -ring-signature $output_dir/bob_ring_signature_file.xf $decoy_str
"
echo ""; echo $str; echo ""
eval $str

# permute ring signature

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -force-write \
    --permute-ring-signature -signer-binomial-key $output_dir/bob_binomial_key_file.xf \
                             -verifier-binomial-key $output_dir/alice_binomial_key_file.xf \
                             -signer-ring-signature $output_dir/bob_ring_signature_file.xf \
                             -verifier-ring-signature $output_dir/alice_ring_signature_file.xf \
                             -permuted-ring-signature $output_dir/permuted_ring_signature_file.xf
"
echo ""; echo $str; echo ""
eval $str

# check ring signature

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/alice_monomial_key_file.xf -ring-signature $output_dir/permuted_ring_signature_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/lib_info_log_file_2.xf \
    --check-ring-signature -plain-text $lorem_ipsum_file -signer-binomial-key $output_dir/signer_binomial_key_file_2.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_log_file_2.xf ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/lib_info_log_file_2.xf\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/signer_binomial_key_file_2.json \
    --print-header -xrn-file $output_dir/signer_binomial_key_file_2.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/bob_binomial_key_file.json \
    --print-header -xrn-file $output_dir/bob_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/bob_binomial_key_file.json $output_dir/signer_binomial_key_file_2.json && echo 'PASS' || echo 'FAIL' ;
"
echo ""; echo $str; echo ""
eval $str

